<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product_model extends CI_Model {

    var $tbl_parent = 'flowers';

    public function __construct() {
        parent::__construct();
        $this->load->helper("delete_img");
    }

    public function add($data) {
        $array = array(
            'PID' => $data['optCat'],
            "menu_id"=>$data["m"],
            'FName' => $data['txtName'],
            'FContent' => $data['content'],
            'FPrice' => $data['txtPrice'],
            'FStatus' => $data['PdStatus'],
            'FPicture' => $data['img'],
            'FNew' => $data['optNewPd'],
            'FMostView' => $data['optMostView'],
            'FSlug' => $data['slug'],
            'FBestSell' => $data['optBestSell']
        );

        $this->db->set($array);
        $q = $this->db->insert('flowers');
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //them danh muc
    public function addDM($data) {
        $array = array(
            "PName" => $data["txtName"],
            "menu_id" => $data["stt"],
            "c_slug" => $data["slug"]
        );

        $this->db->set($array);
        $q = $this->db->insert('category');
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //lay sp va sp xem nhieu
    public function getProduct($id = NULL, $FMV = NULL) {
        if ($FMV === TRUE) {
            $this->db->where('FMostView', 1);
        } else {
            $this->db->where('FID', $id);
        }
        $q = $this->db->get('flowers');
        return $q;
    }

    public function getSameProduct($id, $pid) {
        if ($pid != NULL) {
            $q = $this->db->where("PID", $pid);
        }
        if (isset($id) && $id != NULL) {
            $q = $this->db->where_not_in("FID", $id)->limit(5)->get("flowers");
            if ($q->num_fields() > 0) {
                return $q;
            } else {
                return FALSE;
            }
        }
    }

    public function getProductName($id) {
        $this->db->where('FID', $id);
        $this->db->select('FName');
        $q = $this->db->get('flowers');
        if ($q->num_rows > 0) {
            foreach ($q->result() as $row) {
                $name = $row->FName;
            }
        } else {
            return false;
        }
        return $name;
    }

    public function getProductFromCat($cid = null, $order = null, $limit = null, $offset = null) {
        if ($cid != null) {
            $array = array('PID' => $cid, 'FStatus' => 0);
            $this->db->where($array);
        }
        if ($order != null) {
            $this->db->order_by("FPrice", $order);
        }
        if ($limit != null) {
            $this->db->limit($limit, $offset);
        }
        $q = $this->db->get('flowers');
        return $q;
    }

    public function checkDM($where) {
        if (isset($where)) {
            $q = $this->db->select($where)->group_by($where)->get("flowers");
        }
        return $q;
    }

    public function getProductNew() {
        $this->db->where('FNew', 1);
        $q = $this->db->get('flowers');
        return $q;
    }

    public function getProductBS() {
        $this->db->where("FBestSell", 1);
        $q = $this->db->get('flowers');
        return $q;
    }

    public function getCategoryName($id) {
        $this->db->where('PID', $id);
        $this->db->select('PName');
        $q = $this->db->get('category');
        if ($q->num_rows > 0) {
            foreach ($q->result() as $row) {
                $name = $row->PName;
            }
        } else {
            return false;
        }
        return $name;
    }

    public function GetCategory($id = NULL) {
        if ($id != NULL) {
            $this->db->where("PID", $id);
        }
        $this->db->where('PStatus', 1);
        $query = $this->db->get('category');
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //sua cat
    public function UpdateCat($query, $pid) {
        $data = array(
            'PName' => $query["txtName"],
            "menu_id"=>$query["MStatus"],
            "c_slug" => $query["slug"],
            'PStatus' => $query["CatStatus"]
        );
        if ($pid) {
            $this->db->where("PID", $pid);
        }
        $rs = $this->db->update("category", $data);
        if ($rs)
            return TRUE;
        else
            return FALSE;
    }

    //search va tim danh muc
    public function GetSearchCategory($stt = NULL, $key = NULL, $limit, $offset) {
        $q = $this->db->select('PID,PName,menu_id,PStatus')
                ->from('category')
                ->limit($limit, $offset);

        if ($stt != NULL) {
            $q->where("PStatus", $stt);
        }
        if ($key != NULL) {
            $q->like('PName', $key);
        }
        $ret["rows"] = $q->get();

        $q = $this->db->select('count(*) as count', FALSE)->from("flowers");
        if ($stt != NULL) {
            $q->where("FStatus", $stt);
        }
        if ($key != NULL) {
            $q->like('PName', $key);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }

    public function GetTotalPd($mid) {
        if (isset($mid) && $mid != null) {
            $this->db->where("menu_id", $mid);
        }
        $this->db->where('FStatus', 0);
        $query = $this->db->get('flowers');
        return $query->num_rows();
    }

    public function selectAllP($where = null, $order = null, $limit, $offset) {
        if ($where != null) {
            foreach ($where as $field => $value) {
                if ($field[0] == '?') {
                    $this->db->where_in(substr($field, 1), $value);
                } else if ($field[0] == '!') {
                    $this->db->where_not_in(substr($field, 1), $value);
                } else {
                    $this->db->where($field, $value);
                }
            }
        }
        if ($order != null) {
            $this->db->order_by("FPrice", $order);
        }
        $this->db->limit($limit, $offset);
        $q = $this->db->get('flowers');
        return $q;
    }

    public function GetTotalToCat($pid) {
        $this->db->where('PID', $pid);
        $this->db->from($this->tbl_parent);
        return $this->db->count_all_results();
    }

    public function CountAllSearch($searchparams) {
        $this->db->like($searchparams, '', 'after');
        $this->db->from($this->tbl_parent);
        return $this->db->count_all_results();
    }

    public function GetPageList($limit, $offset) {
        $this->db->order_by('FID', 'asc');
        return $this->db->get($this->tbl_parent, $limit, $offset)->result();
    }

    public function Search($query_array, $limit, $offset) {
        $q = $this->db->select('PID,FID,FName,FContent,FQyty,FPrice,FStatus,FPicture,FNew,FSlug')
                ->from('flowers')
                ->limit($limit, $offset);
//        $q = "select PID,FID,FName,FContent,FQyty,FPrice,FStatus,FPicture,FNew from flowers";
        if (strlen($query_array['cate'])) {
            $q->where('PID', $query_array['cate']);
//            $q .= " where PID =".$query_array['cate'];
        }
        if (isset($query_array['key']) && $query_array['key'] != NULL) {
//            $q->like('FName', $query_array['key']);
            $q->where("MATCH(FName) AGAINST('\"" . $query_array["key"] . "\"' IN BOOLEAN MODE)", NULL, FALSE);
//            $q .= " where MATCH (FName) AGAINST (?) > 0 LIMIT $limit, $offset";
        }
//        $q = $this->db->query($q,array($query_array['key']));
        $ret['rows'] = $q->get()->result();

        $q = $this->db->select('count(*) as count', FALSE)->from('flowers');
//        $q = "select count(*) as count from flowers";
        if (strlen($query_array['cate'])) {
            $q->where('PID', $query_array['cate']);
//            $q .= " where PID = ".$query_array['cate'];
        }
        if (isset($query_array['key']) && $query_array['key'] != NULL) {
//            $q->like('FName', $query_array['key']);
            $q->where("MATCH(FName) AGAINST('\"" . $query_array["key"] . "\"' IN BOOLEAN MODE)", NULL, FALSE);
//            $q.= " where MATCH (FName) AGAINST (?)";
//            $q = $this->db->query($q,array($query_array['key']));
        }
        $tmp = $q->get()->result();
        $ret['num_rows'] = $tmp[0]->count;
        return $ret;
    }

    //save_load query
//    public function save_query($query_array) {
//        $CI = & get_instance();
//        $CI->db->insert('query_str', array('query_string' => http_build_query($query_array)));
//
//        return $CI->db->insert_id();
//    }
//
//    public function load_query($query_id) {
//        $CI = & get_instance();
//
//        $rows = $CI->db->get_where('query_str', array('id' => $query_id))->result();
//        if (isset($rows[0])) {
//            parse_str($rows[0]->query_string, $_GET);
//        }
//    }
    //lay tong sp
    public function SumProduct($fstatus = null) {
        $this->db->select("FID");
        if ($fstatus != null) {
            $this->db->where("FStatus", $fstatus);
        }
        $result = $this->db->get("flowers");
        if ($result->num_rows() > 0) {
            return $result->num_rows();
        } else {
            return $result = 0;
        }
    }

    public function QlAllSp($name = NULL, $stt = NULL, $dm = NULL, $limit, $offset) {
        $q = $this->db->select('PID,FID,FName,FContent,FQyty,FPrice,FStatus,FPicture,FNew,FMostView,FBestSell')
                ->from('flowers')
                ->order_by("FID","desc")
                ->limit($limit, $offset);
        if ($name != NULL) {
            $q->like('FName', $name);
        }
        if ($stt != NULL) {
            $q->where("FStatus", $stt);
        }
        if ($dm != NULL) {
            $q->where("PID", $dm);
        }
        $ret["rows"] = $q->get();

        $q = $this->db->select('count(*) as count', FALSE)->from("flowers");

        if ($name != NULL) {
            $q->like('FName', $name);
        }
        if ($stt != NULL) {
            $q->where("FStatus", $stt);
        }
        if ($dm != NULL) {
            $q->where("PID", $dm);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;

        return $ret;
    }

    //xoa san pham
    public function DeleteSP($fid) {
        $rs = $this->db->where("FID", $fid)->get("flowers");
        if ($rs->num_rows() > 0) {
            $row = $rs->row();
            if (is_file(base_url()."/".$row->FPicture)) {
                unlink(realpath("images/sanpham/" . $row->FPicture));
                $q = $this->db->delete("flowers", array("FID" => $fid));
                return TRUE;
            } else {
                $q = $this->db->delete("flowers", array("FID" => $fid));
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    //xoa danh muc
    public function DeleteDM($pid) {
        $this->db->where("PID", $pid);
        $q = $this->db->delete("category");
        if ($q) {
            return TRUE;
        }else
            return FALSE;
    }

    //sua san pham
    public function UpdatePd($fid, $txtName, $optCat, $m,$txtPrice, $content, $optNewPd, $optBestSell, $optMostView, $PdStatus, $img) {
        $data = array(
            'PID' => $optCat,
            'menu_id'=>$m,
            'FName' => $txtName,
            'FContent' => $content,
            'FPrice' => $txtPrice,
            'FStatus' => $PdStatus,
            'FPicture' => $img,
            'FNew' => $optNewPd,
            'FMostView' => $optMostView,
            'FBestSell' => $optBestSell
        );
        if ($fid) {
            $this->db->where("FID", $fid);
        }
        $rs = $this->db->update("flowers", $data);
        if ($rs)
            return TRUE;
        else
            return FALSE;
    }

}

?>
